public class FromOneTo100 {
    /**
     * 使用wait/notify方法，等待和唤醒线程来完成
     * @param args
     */
    //创建一个锁对象
    public static final  Object Lock = new Object();
    //线程的数量
    public static final int Thread_count = 3;
    //开始
    private static volatile int start = 0;
    //结束
    public static final int end = 100;



    private static class  Print implements Runnable{
        private  final  int index;

        public Print(int index) {
            this.index = index;
        }
        @Override
        public void run() {
            while (start < end){
                synchronized (Lock){
                    while (start % Thread_count != index){
                        try {
                            Lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (start <= end){
                        System.out.println("thread" + (index+1)+ "产生了"+start);

                    }
                    start++;
                    Lock.notifyAll();
                }
            }

        }
    }


    public static void main(String[] args) {
        for (int i = 0; i < Thread_count; i++) {
            new Thread(new Print(i)).start();

        }
    }
}
